package weaver.interfaces.jz.action;

import com.engine.jz.util.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;
import java.math.BigDecimal;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/12/16
 * @Description 人员入职生成工号
 * @Author AdminZm
 */
public class HrmEntryGenerateBHAction implements Action {

    BaseBean baseBean = new BaseBean();

    @Override
    public String execute(RequestInfo requestInfo) {
        RequestManager requestManager = requestInfo.getRequestManager();
        int billId = requestManager.getBillid();
        String tableName = requestManager.getBillTableName();
        String requestId = requestInfo.getRequestid();
        baseBean.writeLog("HrmEntryGenerateBHAction start:" + requestId + "、" + billId + "、" + tableName);
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        try {
            String sfzhm = null;
            String ygxz1 = null;
            String oldGh = null;
            rs.execute("SELECT * FROM " + tableName + " WHERE id = " + billId);
            if (rs.next()) {
                sfzhm = rs.getString("sfzhm");
                ygxz1 = rs.getString("ygxz1");
                oldGh = rs.getString("gh");
            }

            if (StringUtils.isNotEmpty(oldGh)) {
                baseBean.writeLog("HrmEntryGenerateBHAction gh is not null.");
                return Action.SUCCESS;
            }

            if (StringUtils.isEmpty(sfzhm) || StringUtils.isEmpty(ygxz1)) {
                baseBean.writeLog("HrmEntryGenerateBHAction param is null.");
                return Action.SUCCESS;
            }

            // 根据身份证查询原工号
            rs.executeQuery("SELECT workcode FROM hrmresource WHERE certificatenum = ?", sfzhm);
            if (rs.next()) {
                String gh = rs.getString("workcode");
                if (StringUtils.isNotEmpty(gh)) {
                    rs1.executeUpdate("UPDATE " + tableName + " SET gh = ? WHERE id = ?", gh, billId);
                    return Action.SUCCESS;
                }
            }
            String yglbFieldId = rs.getPropValue("jzjg_zm_prop", "yglb_field_id");
            String yglbType = rs.getPropValue("jzjg_zm_prop", "yglb_type");

            String maxGhTemp = "0";
            if (StringUtils.equals(ygxz1, "3")) {
                rs.execute("SELECT a.workcode FROM hrmresource a, cus_fielddata b WHERE a.id = b.id and b.scope = 'HrmCustomFieldByInfoType' " +
                        "and b.scopeid = " + yglbType + " AND a.workcode is not null");
                while (rs.next()) {
                    String ghTemp = rs.getString("workcode");
                    if (StringUtils.isEmpty(ghTemp) || !ghTemp.matches("\\d+")) {
                        continue;
                    }
                    if (ghTemp.length() != 6 || !ghTemp.startsWith("0")) {
                        continue;
                    }
//                    if (ghTemp.length() > 5) {
                        ghTemp = ghTemp.substring(ghTemp.length() - 5);
//                    }
                    ghTemp = CommonUtil.removeLeadingZeros(ghTemp);
                    if (StringUtils.isEmpty(ghTemp)) {
                        continue;
                    }
                    if (new BigDecimal(ghTemp).compareTo(new BigDecimal(maxGhTemp)) >= 0) {
                        maxGhTemp = ghTemp;
                    }
                }

                // 查询在途流程
                String sql = "select a.* from " + tableName + " a, workflow_requestbase b WHERE a.requestId = b.requestId and " +
                        "b.currentnodetype != 3 ";
                rs.execute(sql);
                while (rs.next()) {
                    String requestIdT = rs.getString("requestId");
                    if (StringUtils.isEmpty(requestIdT) || StringUtils.equals(requestIdT, requestId)) {
                        continue;
                    }
                    String ghT = rs.getString("gh");
                    if (StringUtils.isEmpty(ghT) || !ghT.matches("\\d+")) {
                        continue;
                    }
                    if (ghT.length() != 6 || !ghT.startsWith("0")) {
                        continue;
                    }
//                    if (ghT.length() >= 5) {
                        ghT = ghT.substring(ghT.length() - 5);
//                    }
                    ghT = CommonUtil.removeLeadingZeros(ghT);
                    if (StringUtils.isEmpty(ghT)) {
                        continue;
                    }
                    if (new BigDecimal(ghT).compareTo(new BigDecimal(maxGhTemp)) >= 0) {
                        maxGhTemp = ghT;
                    }
                }
            } else {
                rs.execute("SELECT a.workcode FROM hrmresource a, cus_fielddata b WHERE a.id = b.id and b.scope = 'HrmCustomFieldByInfoType' " +
                        " and scopeid = " + yglbType +
                        " AND a.workcode is not null");
                while (rs.next()) {
                    String ghTemp = rs.getString("workcode");
                    if (StringUtils.isEmpty(ghTemp) || !ghTemp.matches("\\d+")) {
                        continue;
                    }
                    if (ghTemp.length() != 6 || !ghTemp.startsWith("9")) {
                        continue;
                    }

                    ghTemp = ghTemp.substring(ghTemp.length() - 5);

                    ghTemp = CommonUtil.removeLeadingZeros(ghTemp);
                    if (StringUtils.isEmpty(ghTemp)) {
                        continue;
                    }
                    if (new BigDecimal(ghTemp).compareTo(new BigDecimal(maxGhTemp)) >= 0) {
                        maxGhTemp = ghTemp;
                    }
                }

                // 查询在途流程
                String sql = "select a.* from " + tableName + " a, workflow_requestbase b WHERE a.requestId = b.requestId and " +
                        "b.currentnodetype != 3 ";
                rs.execute(sql);
                while (rs.next()) {
                    String requestIdT = rs.getString("requestId");
                    if (StringUtils.isEmpty(requestIdT) || StringUtils.equals(requestIdT, requestId)) {
                        continue;
                    }
                    String ghT = rs.getString("gh");
                    if (StringUtils.isEmpty(ghT) || !ghT.matches("\\d+")) {
                        continue;
                    }
                    if (ghT.length() != 6 || !ghT.startsWith("9")) {
                        continue;
                    }
                    ghT = ghT.substring(ghT.length() - 5);

                    ghT = CommonUtil.removeLeadingZeros(ghT);
                    if (StringUtils.isEmpty(ghT)) {
                        continue;
                    }
                    if (new BigDecimal(ghT).compareTo(new BigDecimal(maxGhTemp)) >= 0) {
                        maxGhTemp = ghT;
                    }
                }
            }

            if (StringUtils.isEmpty(maxGhTemp)) {
                maxGhTemp = "0";
            }

            String gh = CommonUtil.getNext(Integer.valueOf(maxGhTemp), 5);
            if (StringUtils.equals(ygxz1, "3")) {
                rs1.executeUpdate("UPDATE " + tableName + " SET gh = ? WHERE id = ?", "0" + gh, billId);
            } else {
                rs1.executeUpdate("UPDATE " + tableName + " SET gh = ? WHERE id = ?", "9" + gh, billId);
            }
            baseBean.writeLog("HrmEntryGenerateBHAction end.");
        } catch (Exception e) {
            baseBean.writeLog("HrmEntryGenerateBHAction error:" + e);
        }
        return Action.SUCCESS;
    }
}
